﻿using System;
using System.Collections.Generic;
using Ur;
using Sargon;

namespace Pixels.Interface
{
    public abstract class InterfaceElement
    {
        public InterfaceElement() {
            children = new List<InterfaceElement>();
            Interface.Instance.elementAdded(this);
            visible = true;
            
        }
        public Rect localPosition;

        public abstract void render();
        
        public abstract void input(InputState state);
        
        public abstract void tick();

        public InterfaceElement         parent { get; private set;}
        public List<InterfaceElement> children { get; private set; }
        public void die() {
            foreach (var child in children.ToArray()) child.die();
            if (parent != null) parent.children.Remove(this);
            Interface.Instance.elementRemoved(this);
        }

        public void attachToParent(InterfaceElement parent) {
            this.parent = parent;
            parent.children.Add(this);
            this.zed = parent.zed + 1;
        }

        public Rect ScreenRect {
            get {
                if (parent == null) return localPosition;
                return new Rect(parent.ScreenRect.x0 + localPosition.x0, parent.ScreenRect.y0 + localPosition.y0, localPosition.w, localPosition.h);
            }
        }
        public virtual bool respondsToHilite { get { return false; } }

        internal bool IsHilit { get { return Interface.Instance.hilitElement == this; } }

        public int zed = 0;

        public bool Visible {
            get {
                if (parent == null) return this.visible;
                return this.visible && parent.Visible;
            }
        }

        public bool visible;

        public Tooltip tooltip = new Tooltip("", "");
    }
}
